// https://leetcode.cn/problems/happy-number/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    bool isHappy(int n) {
        unordered_set<int> mp;

        int num = n;
        while (true) {
            int tmp = num;
            int sum = 0;
            while (tmp) {
                sum += (tmp % 10) * (tmp % 10);
                tmp /= 10;
            }

            if (sum == 1) {
                return true;
            } else if (mp.count(sum)) {
                return false;
            } else {
                mp.insert(sum);
                num = sum;
            }
        }
    }
};